//
//  ChangePassWordVC.m
//  xpkc
//
//  Created by TopSage OS X on 15/11/30.
//  Copyright © 2015年 TopSage. All rights reserved.
//

#import "ChangePassWordVC.h"
#import "JMTextField.h"
#import "UserMessage.h"
@interface ChangePassWordVC ()<UITextFieldDelegate>

@property (nonatomic,strong)JMTextField * oldPassword;

@property (nonatomic,strong)JMTextField * NewWord;

@property (nonatomic,strong)JMTextField * againPassword;

@end

@implementation ChangePassWordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"修改密码";
    self.view.backgroundColor=BGColor;
    [self createUI];
    [self creatNavItem];
    
    
}
-(void)creatNavItem{
    UIImage *image = [UIImage imageNamed:@"back1"];
    if (IsIOS7) {
        image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStyleBordered target:self action:@selector(backtolistentest)];
    self.navigationItem.leftBarButtonItem = backItem;
}
-(void)backtolistentest{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)createUI{
    _oldPassword = [[JMTextField alloc]initWithFrame:CGRectMake(15, 30 , SCREEN_WIDTH - 30, 50 *SCREEN_HEIGHT/667)];
    
    _oldPassword.backgroundColor = [UIColor whiteColor];
    
    _oldPassword.placeholder = @"原密码";
    _oldPassword.secureTextEntry=YES;
    [self.view addSubview:_oldPassword];
    
    _NewWord = [[JMTextField alloc]initWithFrame:CGRectMake(15, 30 + (50 *SCREEN_HEIGHT/667 +10) , SCREEN_WIDTH - 30, 50 *SCREEN_HEIGHT/667)];
    _NewWord.backgroundColor = [UIColor whiteColor];
    _NewWord.secureTextEntry=YES;
    _NewWord.placeholder = @"新密码";
    
    [self.view addSubview:_NewWord];
    
    _againPassword = [[JMTextField alloc]initWithFrame:CGRectMake(15, 30 + (50 *SCREEN_HEIGHT/667 +10)*2 , SCREEN_WIDTH - 30, 50 *SCREEN_HEIGHT/667)];
    
    _againPassword.backgroundColor = [UIColor whiteColor];
    _againPassword.secureTextEntry=YES;
    _againPassword.placeholder = @"确认密码";
    
    [self.view addSubview:_againPassword];
    
    
    
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setTitle:@"确 认" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor orangeColor];
    [button setTintColor:[UIColor whiteColor]];
    button.layer.cornerRadius = 10;
    button.frame = CGRectMake(15 , 30 + (50 * SCREEN_HEIGHT/667 +10) *2 +40 + 50 * SCREEN_HEIGHT/667, SCREEN_WIDTH - 30, 60 *SCREEN_HEIGHT/667);
    [button addTarget:self action:@selector(changeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:button];
    
    
    
    
}


- (BOOL)checkValidityTextField
{
    
    
    if ([_oldPassword.text isEqualToString:@""]||_oldPassword.text==nil) {
        ShowAlertMsg(@"原密码不能为空");
        return NO;
    }
    if  ([_NewWord.text isEqualToString:@""]||_NewWord.text==nil) {
        ShowAlertMsg(@"新密码不能为空");
        
        return NO;
    }
    if  ([_againPassword.text isEqualToString:@""]||_againPassword.text==nil){
        ShowAlertMsg(@"确认密码不能为空");
        return NO;
    }
    if  (![_againPassword.text isEqualToString:_NewWord.text]) {
        ShowAlertMsg(@"密码输入不一致");
        
        return NO;
    }
    
    
    return YES;
    
}



-(void)changeBtnClick{
    
    [_againPassword resignFirstResponder];
    
    [_oldPassword resignFirstResponder];
    
    [_NewWord resignFirstResponder];
    
    
    if ([self checkValidityTextField]) {
        
    
    
    //获取数据
    NSString *module = @"userLogin";
    NSString *interface = @"modifpswd";
    NSString *timeString = [GetTime getTime];
    NSString *hashString = [[NSString stringWithFormat:@"%@/%@/key/%@/time/%@",module,interface,KEY,timeString] md5];
    NSString *urlString = [NSString stringWithFormat:@"%@/laravel/public/%@/%@/hash/%@/appid/1",URL2,module,interface,hashString];
    NSDictionary *parameter = @{@"time":timeString,@"userName":[UserMessage sharedUserMessage].userId,@"userPWD":_oldPassword.text,@"NuserPWD":_NewWord.text};
   
    //POST请求
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
    [manager POST:urlString parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
       
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        NSLog(@"%@",dic);
       if ([[dic objectForKey:@"succ"] isEqualToNumber:@1]) {
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"密码修改成功" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
            [alert show];
        
           [UserMessage sharedUserMessage].isLogin = NO;
           [[NSUserDefaults standardUserDefaults]setValue:_againPassword.text forKey:PASSWORD_REMBER];

           [self.navigationController popToRootViewControllerAnimated:YES];
       }else{
           UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"密码修改失败" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
           
           [alert show];
       }
        
  } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        logdebug(@"%@",error.localizedDescription);
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"密码修改失败" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
        
        [alert show];
        
    }];
    
}
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_oldPassword resignFirstResponder];
    [_NewWord resignFirstResponder];
    [_againPassword resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
